package com.myk.game.heroscuffle.skills;

import com.myk.game.heroscuffle.events.EventRunBack;
import com.myk.game.heroscuffle.events.HeroEvent_SufferDamage;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.game.Skill;

public class Skill_TongXin extends Skill {

    public static final int HEALING_AMOUNT = 30;
    public static final int TEMP_DEF = 20;

    private Hero intentTarget;

    public void setIntentTarget(Hero intentTarget) {
        this.intentTarget = intentTarget;
    }

    public Skill_TongXin() {
        super("同心", "指定一位英雄与自己一起，恢复少量生命值、临时增加防御力、免疫技能和标记伤害、且互相消除仇恨。", 2, 5);
    }

    @Override
    protected void onTrigger() {
        if (hostHero.state.isPlayer) {
            selectTargetHero(0, true);
        }
        else {
            if (intentTarget == null) {
                intentTarget = game.pool.getAnyoneAliveHero(hostHero);
            }
            skillEffect(intentTarget);
        }
    }

    //玩家选择技能施放目标
    private void selectTargetHero(final int index, final boolean isFirst) {
        game.executor.submit(() -> {
            Hero curSelectHero = game.pool.getAllAliveHeroes().get(index);
            if (curSelectHero == hostHero) {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }
            game.ui.setTip("请选择你要治疗的目标,当前待选:" + curSelectHero.getObviousName());
            game.ui.resetCtrl();
            game.ui.addCtrl("选中", () -> game.executor.submit(() -> {
                skillEffect(curSelectHero);
                game.executor.resume(); //通知阻塞的线程继续执行以后的代码
            }));
            game.ui.addCtrl("切换", () -> game.executor.submit(() -> {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }));
        });
        if (isFirst) { //防止因为多次切换而阻塞多个线程
            game.executor.pause(); //阻塞线程直到接收到通知
        }
    }

    //技能效果
    private void skillEffect(Hero target) {
        if (target.state.getAliveEnemy() == hostHero) {
            target.clearEnemy();
        }
        if (target.state.getAlivePrey() == hostHero) {
            target.clearPrey();
        }
        if (hostHero.state.getAliveEnemy() == target) {
            hostHero.clearEnemy();
        }
        if (hostHero.state.getAlivePrey() == target) {
            hostHero.clearPrey();
        }
        game.ui.printMsg("与" + target.getObviousName() + "握手言和,共同免疫技能伤害一回合。");
        game.ui.sleepShortly();
        //目标拥有的效果
        game.ui.printMsg(target.getObviousName());
        target.addHpWithPrint(HEALING_AMOUNT); //恢复生命值
        game.ui.printMsg("临时");
        target.addDefWithPrint(TEMP_DEF); //临时增加防御力
        EventRunBack<HeroEvent_SufferDamage> targetImmuneRunBack = eventData -> {
            if (eventData.sourceSkill != null || eventData.sourceSign != null) {
                game.ui.printMsg(target.getObviousName() + "免疫该伤害,");
            }
        }; //收到技能和标记伤害时免疫掉的监听
        target.event.onSufferDamage.addRunBack(targetImmuneRunBack); //添加监听到身上
        target.event.onAfterAction.addRunBack(eventData -> {  //回合结束时监听到身上
            game.ui.printMsg(target.getObviousName());
            target.dropDefWithPrint(TEMP_DEF); //还原防御力
            target.event.onSufferDamage.removeRunBack(targetImmuneRunBack); //移除免疫技能伤害的监听
            eventData.removeRunBackFromListener(); //移除自身监听
        });
        //自己也有同样的效果
        game.ui.printMsg(hostHero.getObviousName());
        hostHero.addHpWithPrint(HEALING_AMOUNT); //恢复生命值
        game.ui.printMsg("临时");
        hostHero.addDefWithPrint(TEMP_DEF); //临时增加防御力
        EventRunBack<HeroEvent_SufferDamage> ownImmuneRunBack = eventData -> {
            if (eventData.sourceSkill != null || eventData.sourceSign != null) {
                game.ui.printMsg(hostHero.getObviousName() + "免疫该伤害,");
            }
        }; //受到技能和标记伤害时免疫掉的监听
        hostHero.event.onSufferDamage.addRunBack(ownImmuneRunBack); //添加监听到身上
        int currentRound = game.round;
        hostHero.event.onAfterAction.addRunBack(eventData -> {  //回合结束时监听到身上
            if (game.round > currentRound) { //当前回合不生效，下回合才才会进行还原
                game.ui.printMsg(hostHero.getObviousName());
                hostHero.dropDefWithPrint(TEMP_DEF); //还原防御力
                hostHero.event.onSufferDamage.removeRunBack(ownImmuneRunBack); //移除免疫技能伤害的监听
                eventData.removeRunBackFromListener(); //移除自身监听
            }
        });
    }

    @Override
    protected void onUpdate() {

    }

    @Override
    protected void onInit() {

    }
}
